package com.example.es;

import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Author：wlz
 * @Package：com.example.es
 * @Project：redis
 * @name：ESTest_Index_Create
 * @Date：2023/4/15 22:18
 * @Filename：ESTest_Index_Create
 */
public class ESTest_Doc_Insert_Batch {
    public static void main(String[] args) throws IOException {
        //创建
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200)));
        //批量插入数据
        BulkRequest request = new BulkRequest();

        request.add(new IndexRequest().index("user1").id("1001").source(XContentType.JSON,"name","lisi","age",15,"sex","男"));
        request.add(new IndexRequest().index("user1").id("1002").source(XContentType.JSON,"name","zhaoliu","age",15,"sex","男"));
        request.add(new IndexRequest().index("user1").id("1003").source(XContentType.JSON,"name","mafei","age",15,"sex","女"));
        request.add(new IndexRequest().index("user1").id("1003").source(XContentType.JSON,"name","fagg","age",15,"sex","男"));
        request.add(new IndexRequest().index("user1").id("1003").source(XContentType.JSON,"name","dgg","age",150,"sex","女"));


        BulkResponse index = client.bulk(request, RequestOptions.DEFAULT);

        //响应状态
        System.out.println(index.getTook());
        System.out.println(Arrays.toString(index.getItems()));



        //关闭
        client.close();
    }
}
